package com.web.activiti.utils;

import com.web.activiti.pojo.ActiveUser;
import com.web.activiti.pojo.Employee;
import com.web.activiti.service.EmployeeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class TaskAssigneeListener implements TaskListener {

	@Override
	public void notify(DelegateTask task) {
		//调用业务层
		//通过硬编码获取spring容器
		WebApplicationContext context= ContextLoader.getCurrentWebApplicationContext();
		EmployeeService employeeService = (EmployeeService) context.getBean("EmployeeService");
		//获取当前登录人的信息  并且获得他的部门经理的id
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpSession session = servletRequestAttributes.getRequest().getSession();
		ActiveUser employee = (ActiveUser) session.getAttribute(Contants.GLOBAL_SESSION_ID);
		System.out.println("notify..."+employee.getManagerId());
		Employee manager = employeeService.findManager(employee.getManagerId());
		//设置代办人
		System.out.println(manager.getName());
		task.setAssignee(manager.getName());
	}

}
